class CustomerController 
{
    def jdbcTmplt;
    
    def index = { redirect(action:list,params:params) }

    static allowedMethods = [save:'POST', update:'POST']

    def list = 
    {	
        if(!params.max) params.max = com.util.DictUtil.PARAMSMAX;
        if(!params.offset) params.offset = 0;
        def nametj = "";
        def customerInstanceList = [];
        def totalRow;   
        if(params.nametj)
        	nametj = params.nametj;
        if(nametj && nametj != "")
        {
        	if(params.sort && params.order)
        	{
        		customerInstanceList = Customer.findAll("from Customer where custName like '%${nametj}%' order by ${params.sort} ${params.order}", [max:params.max.toInteger(), offset:params.offset.toInteger()]);
        	}else
        	{
        		customerInstanceList = Customer.findAll("from Customer where custName like '%${nametj}%'", [max:params.max.toInteger(), offset:params.offset.toInteger()]);
        	}
        	totalRow = Customer.executeQuery("select count(*) from Customer where custName like '%${nametj}%'");
        	totalRow = totalRow[0];       	
        }else
        {
        	if(params.sort && params.order)
        	{
        		customerInstanceList = Customer.findAll("from Customer order by ${params.sort} ${params.order}", [max:params.max.toInteger(), offset:params.offset.toInteger()]);
        	}else
        	{
        		customerInstanceList = Customer.findAll("from Customer", [max:params.max.toInteger(), offset:params.offset.toInteger()]);
        	}
        	totalRow  = Customer.executeQuery("select count(*) from Customer");
        	totalRow = totalRow[0];
        }
        
        return [ customerInstanceList:customerInstanceList, totalRow:totalRow, nametj:nametj, max:params.max, offset:params.offset, sort:params.sort, order:params.order ]
    }
    
	def create =
	{
		def customerInstance = new Customer()
		customerInstance.properties = params
		return ['customerInstance':customerInstance]
	}

	def save =
	{
		def customerInstance = new Customer(params)
		if(!customerInstance.hasErrors() && customerInstance.save(flush:true))
		{
			flash.message = message(code:'lxh.display.createMsg')
			redirect(action:list)
		}else
		{
			render(view:'create',model:[customerInstance:customerInstance])
		}
	}
	
    def show = 
    {
        def customerInstance = Customer.get( params.id )

        if(!customerInstance) 
        {
            flash.message = message(code:'lxh.display.noFindMsg')
            redirect(action:list)
        }
        else { return [ customerInstance : customerInstance ] }
    }

    def edit = 
    {
        def customerInstance = Customer.get( params.id )

        if(!customerInstance) 
        {
            flash.message = message(code:'lxh.display.noFindMsg')
            redirect(action:list)
        }else 
        {
            return [ customerInstance : customerInstance ]
        }
    }

    def update = 
    {
        def customerInstance = Customer.get( params.id )
        if(customerInstance) 
        {
            customerInstance.properties = params
            if(!customerInstance.hasErrors() && customerInstance.save(flush:true)) 
            {
                flash.message = message(code:'lxh.display.updateMsg')
                redirect(action:list)
            }else 
            {
                render(view:'edit',model:[customerInstance:customerInstance])
            }
        }else 
        {
            flash.message = message(code:'lxh.display.noFindMsg')
            redirect(action:edit,id:params.id)
        }
    }

	def delete =
	{
		def customerInstance = Customer.get( params.id )
		if(customerInstance)
		{
			try
			{
				customerInstance.delete(flush:true)
				flash.message = message(code:'lxh.display.deleteMsg')
			}catch(Exception e)
			{
				flash.message = message(code:'lxh.display.noDelMsg');
			}
			redirect(action:list)
		}else
		{
			flash.message = message(code:'lxh.display.noFindMsg')
			redirect(action:list)
		}
	}
    
    def delBatch = 
    {			
    	def batchIds = params.batchIds;
		try
		{
			jdbcTmplt.update("delete from customer where id in (${batchIds})");
			flash.message = message(code:'lxh.display.deleteMsg')
			
		}catch(Exception e)
		{
			flash.message = message(code:'lxh.display.noDelMsg');
		}
    	redirect(action:list);
    }
	
	
	
	
}